timers = [ \
              "overall", \
         ]

# create timer header file
f = open("src/timing.h","w")
f.write("#ifndef PCPS_TIMING_HEADER\n")
f.write("#define PCPS_TIMING_HEADER\n")
f.write("\n")
f.write("#include <src/pcps.h>\n")
f.write("\n")
f.write("namespace pcps {\n")
f.write("\n")
f.write("  void initialize_timers();\n")
f.write("  void finalize_timers();\n")
f.write("\n")
for t in timers:
  f.write("  void start_%s_timer(const int thread_id);\n" % t)
f.write("\n")
for t in timers:
  f.write("  void stop_%s_timer(const int thread_id);\n" % t)
f.write("\n")
for t in timers:
  f.write("  const pcps::Stopwatch & get_%s_timer();\n" % t)
f.write("\n")
f.write("} // end namespace pcps\n")
f.write("\n")
f.write("#endif\n")
f.close()

# create timer cpp file
f = open("src/timing.cpp","w")
f.write("#include <src/pcps.h>\n")
f.write("#include <src/timing.h>\n")
f.write("\n")
f.write("namespace pcps {\n")
f.write("\n")
f.write("  namespace timers {\n")
f.write("\n")
f.write("    pthread_mutex_t mutex;\n")
f.write("\n")
#f.write("    inline void start(double & t) {\n")
#f.write("      //pthread_mutex_lock(&pcps::timers::mutex);\n")
#f.write("      t -= MPI::Wtime();\n")
#f.write("      //pthread_mutex_unlock(&pcps::timers::mutex);\n")
#f.write("    }\n")
#f.write("\n")
#f.write("    inline void stop(double & t) {\n")
#f.write("      //pthread_mutex_lock(&pcps::timers::mutex);\n")
#f.write("      t += MPI::Wtime();\n")
#f.write("      //pthread_mutex_unlock(&pcps::timers::mutex);\n")
#f.write("    }\n")
#f.write("\n")
for t in timers:
  f.write("    pcps::Stopwatch %s_time;\n" % t)
f.write("\n")
f.write("  }\n")
f.write("\n")
f.write("}\n")
f.write("\n")
f.write("void pcps::initialize_timers() {\n")
f.write("  pthread_mutex_init(&pcps::timers::mutex, NULL);\n")
for t in timers:
  f.write("  pcps::timers::%s_time.reset();\n" % t)
f.write("}\n")
f.write("\n")
f.write("void pcps::finalize_timers() {\n")
f.write("  pthread_mutex_destroy(&pcps::timers::mutex);\n")
#f.write("  //std::cout << std::endl;\n")
#for t in timers:
#  f.write("  //std::cout << boost::format(\"%%30s = %%20.6f\") %% \"%s\" %% pcps::timers::%s_time << std::endl;\n" %(t,t))
f.write("}\n")
f.write("\n")
for t in timers:
  f.write("void pcps::start_%s_timer(const int thread_id) { if (thread_id == 0) pcps::timers::%s_time.start(); }\n" %(t,t))
f.write("\n")
for t in timers:
  f.write("void pcps::stop_%s_timer(const int thread_id) { if (thread_id == 0) pcps::timers::%s_time.stop(); }\n" %(t,t))
f.write("\n")
for t in timers:
  f.write("const pcps::Stopwatch & pcps::get_%s_timer() { return pcps::timers::%s_time; }\n" % (t,t))
f.write("\n")
f.close()
